<?php
//
header("content-type:text/html;charset=utf-8");

session_start();
$link = @mysqli_connect('localhost','root','root','db_info');
if(empty($link)){
    echo '数据库连接失败';
    die();
}
mysqli_query($link,"set names 'utf8'");
date_default_timezone_set("PRC");
$data = $_POST;
include_once '../db.php';

switch ($data['type']){
    case 'adminLogin':
        login();
        break;
    case 'logout':
        logout();
        break;
    case 'changePwd':
        changePwd();
        break;
    case 'delUser':
        delUser();
        break;
    case 'modify':
        modify();
        break;
    case 'addNewUser':
        addNewUser();
        break;
    case 'getArc':
        getArc();
        break;
    case 'modifyArt':
        modifyArt();
        break;
    case 'delArt':
        delArt();
        break;
    case 'newArt':
        newArt();
        break;
    case 'newSub':
        newSub();
        break;
    case 'delSub':
        delSub();
        break;
    default:
        echo '<h1 style="text-align: center;">参数错误</h1>';
        die();
}

//登录 返回状态：2 = 不是管理员账户  1 = 登录成功 0 = 登录失败
function login(){
    global $data;
    $user = getList('user','username = "'.$data['username'].'"');
    $username = $data['username'];
    $password = md5($data['password']);
//        die();
    if(empty($user)){
        echo 0;
        die();
    }

    $currentUsername = $user[0]['username'];
    $currentPassword = $user[0]['password'];
    $isAdmin = $user[0]['isAdmin'];
    if($currentUsername === $username && $currentPassword === $password){
        if($isAdmin !== '1'){
           echo 2;
           die();
        }
        $_SESSION['adminUsername'] = $currentUsername;
        $_SESSION['adminUid'] = $user[0]['uid'];
        echo 1;
    }else{
        echo 0;
    }
}
//修改密码 2 = 旧密码不一致  3 = 密码与重复密码不一致 1 = 成功  0 = 意外的未知错误
function changePwd(){
    global $data;
    $nowPwd = md5($data['password']);
    $newPwd = md5($data['newPassword']);
    $rePwd = md5($data['okPassword']);
    $currentUser = getOne('user',"uid = '".$_SESSION['adminUid']."'");
    if($nowPwd !== $currentUser['password']){
        echo 2;
        die();
    }
    if($newPwd !== $rePwd){
        echo 3;
        die();
    }
    $update = update('user',[
        'password' => $newPwd
    ],"uid = '".$_SESSION['adminUid']."'");
    if(!empty($update)){
        echo 1;
        session_destroy();
    }else{
        echo 0;
    }
}

//删除用户 0 = 失败  1 = 成功
function delUser(){
    global $data;
    $res = del('user',"uid = '".$data['uid']."'");
    if(empty($res)){
        echo 0;
    }else{
        echo 1;
    }
}
//添加新用户 2 = 有数据为空  3 = 重复用户名
function addNewUser(){
    global $data;
    if($data['username'] === '' || $data['password'] === '' || $data['email'] === ''){
        echo 2;
        die();
    }
    $onlyOne = getOne('user',"username = '".$data['username']."'");
//        echo $onlyOne;
//        die();
    if(!empty($onlyOne)){
        echo 3;
        die();
    }
    $result = add('user',[
        'username' => $data['username'],
        'password' => md5($data['password']),
        'email' => $data['email'],
        'isAdmin' => $data['isAdmin'],
        'isBanned' => $data['isBanned'],
    ]);
    if($result){
        echo 1;
    }else{
        echo 0;
    }
}

function modify(){
    global $data;
//    print_r($data);
      $filter = array_filter ($data,function($v){if($v==''){return false;}else{return true;}});
//    print_r($filter);

    $targetuser = $filter['targetuid'];
    unset($filter['targetuid']);
    unset($filter['type']);
    if(!empty($filter['password'])){
        $filter['password'] = md5($filter['password']);
    }
//    print_r($filter);
//    die();
    $result = update('user',$filter,"uid = '".$targetuser."'");
    $res = [];
    $res['status'] = $result;
    $res['newUserInfo'] = getOne('user',"uid = '".$data['targetuid']."'");
    echo json_encode($res);
}
function getArc(){
    global $data;
    $res = getOne('article',"id = '".$data['id']."'");
    echo $res['text'];
}
function modifyArt(){
    global $data;
    $targetid = $data['targetid'];
    $title = $data['title'];
    $isRecommend = $data['isRecommend'];
    $text = $data['text'];
    $res = update('article',[
        'title' => $title,
        'isRecommend' => $isRecommend,
        'text' => $text
    ],"id = '".$targetid."'");
    $return = [];
    $return['status'] = $res;
    $return['newArticle'] = getOne('article',"id = '".$targetid."'");
    echo json_encode($return);
}
function delArt(){
    global $data;
    $delArt = $data['id'];
    $res = del('article',"id = '".$delArt."'");
    if(empty($res)){
        echo 0;
    }else{
        echo 1;
    }
}
function newArt(){
    global $data;
    $title = $data['title'];
    $isRecommend = $data['isRecommend'];
    $text = $data['text'];

    $result = add('article',[
        'title' => $title,
        'isRecommend' => $isRecommend,
        'text' => $text,
        'user_id' => $_SESSION['adminUid']
    ]);
    if($result){
        echo 1;
    }else{
        echo $result;
    }
}

function newSub(){
    global $data;
    $email = $data['email'];
    $res = add('submit',[
           'email' => $email
    ]);
    if(empty($res)){
     echo 0;
    }else{
     echo 1;
    }
}
function delSub(){
    global $data;
    $sub_id = $data['id'];
    $res = del('submit',"sub_id = '$sub_id'");
    if(empty($res)){
        echo 0;
    }else{
        echo 1;
    }
}
function logout(){
    session_destroy();
    echo 1;
}